package com.xuhuan.thread.study2.syn;

/**
 * @author huan.xu
 * @date 2021/9/29 17:40
 * @description 简单死锁案例
 */
public class DeadLockDemo {

    private static Object obj1 = new Object();
    private static Object obj2 = new Object();

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (obj1) {
                System.out.println(Thread.currentThread().getName() + " lock obj1");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (obj2) {
                    System.out.println(Thread.currentThread().getName() + " lock obj2");
                }
            }

        }, "线程1").start();

        new Thread(() -> {
            synchronized (obj2) {
                System.out.println(Thread.currentThread().getName() + " lock obj2");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (obj1) {
                    System.out.println(Thread.currentThread().getName() + " lock obj1");
                }
            }
        }, "线程2").start();
    }
}
